package com.bdqn.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.entity.UmsUser;
import com.bdqn.mapper.UmsUserMapper;
import com.bdqn.service.UmsUserService;
import com.bdqn.util.Pet_service_RedisTemplate;
import com.bdqn.util.TokenUtil;
import com.bdqn.util.Token_Redis_key;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author hunter
 * @since 2022-04-30
 */
@Transactional
@Service
public class UmsUserServiceImpl extends ServiceImpl<UmsUserMapper, UmsUser> implements UmsUserService {
    @Autowired
    private UmsUserMapper umsUserMapper;
    //把自定义的工具对象注入进来
    @Autowired
    private Pet_service_RedisTemplate pet_service_redisTemplate;

    //在service实现类中完成登录用户的逻辑操作  并生成随机的token 存进redis 返回给前台
    @Override
    public String loginUser(String userName, String password) {
        UmsUser umsUser = umsUserMapper.loginUser(userName, password);
        if (umsUser != null) {
            //说明查询出来用户 ==>登录成功   生成随机的token(令牌)
            String token = TokenUtil.token();//获取token
            String redisKey = Token_Redis_key.generateTokenRedisKey(token);
            //把token存到redis数据库
          pet_service_redisTemplate.setToken(redisKey, umsUser);
          //返回token
            return token;
        }
        return null;
    }
   //退出登录
    @Override
    public Boolean loginOut(String token) {
        UmsUser user = pet_service_redisTemplate.getUser(token);
        if(user==null){
            //说明token不对
            return false;
        }else {
            pet_service_redisTemplate.delete(token);
            return true;
        }
    }
//    通过token获取用户信息
    @Override
    public UmsUser getUserByToken(String token) {
        UmsUser user = pet_service_redisTemplate.getUser(token);
        return user;
    }
}